package com.sinodata.bsm.cicp.collector.common;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.httpclient.HttpClient;
import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * <p>
 * Description: 
 * </p>
 *
 * @author wangyilin
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * Dec 19, 2012 5:27:59 PM          wangyilin        1.0         To create
 * </p>
 *Http模拟类
 * @since 
 * @see
 */
public class HttpMockCollector extends Collector {
    private static final Logger logger = Logger.getLogger(HttpMockCollector.class);

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        String classname = this.getParamMap().get("classname");
        try {
            AbstractHttpMock mock = (AbstractHttpMock) Class.forName(classname).newInstance();
            String parameters = this.getParamMap().get("parameters");
            mock.setParameters(parameters);
            mock.execute();
            ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[4];

            propValues[0] = ResPropertyValue.createPropValue(String.valueOf(mock.getRespondTime()), getPropMeta()[0]);
            propValues[1] = ResPropertyValue.createPropValue(mock.getReturnVal(), getPropMeta()[1]);
            propValues[2] = ResPropertyValue.createPropValue(mock.getErrorMsg(), getPropMeta()[2]);
            ResPropertyValue propertyValueX = new ResPropertyValue();
            propertyValueX.setValues(propValues);
            List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
            values.add(propertyValueX);
            return values;
        } catch (Exception e) {
            logger.error("error collect http mock", e);
        }
        return null;
    }

    /**
     * 定义类名和参数集。
     * 参数的格式不限制，在各个具体实现类中解析
     */
    @Override
    public String[] getParamMeta() {
        return new String[] { "classname", "parameters" };
    }

    @Override
    public String[] getPropMeta() {
        //1： 响应时间, 2: 运行结果, 3:异常信息
        return new String[] { "$1", "$2", "$3" };
    }

}
